#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

class KernelELU {
public:
    __aicore__ inline KernelELU() {}

    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, float negativeSlope)
    {
        this->negativeSlope = static_cast<float>(negativeSlope);

        xGm.SetGlobalBuffer((__gm__ float *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ float *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuf0, TILE_LENGTH * sizeof(uint8_t));
        pipe.InitBuffer(tmpBuf1, TILE_LENGTH * sizeof(float));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; ++i) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();
        AscendC::LocalTensor<uint8_t> selector = tmpBuf0.Get<uint8_t>();
        AscendC::LocalTensor<float> tmptensor0 = tmpBuf1.Get<float>();

        float scalar = 0.0;
        float bias = -1.0;
        AscendC::CompareScalar(selector, xLocal, scalar, AscendC::CMPMODE::LT, TILE_LENGTH);  // x < 0

        AscendC::Exp(tmptensor0, xLocal, TILE_LENGTH);
        AscendC::Adds(tmptensor0, tmptensor0, bias, TILE_LENGTH);
        AscendC::Muls(tmptensor0, tmptensor0, this->negativeSlope, TILE_LENGTH);  // a(exp(x)-1)

        AscendC::Select(zLocal, selector, tmptensor0, xLocal, AscendC::SELMODE::VSEL_TENSOR_TENSOR_MODE, TILE_LENGTH);

        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
    }

    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    float negativeSlope;

    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> zGm;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0, tmpBuf1;
};

extern "C" __global__ __aicore__ void elu_custom(GM_ADDR x, GM_ADDR z, float negativeSlope)
{
    KernelELU op;
    op.Init(x, z, negativeSlope);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void elu_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *z, float negativeSlope)
{
    elu_custom<<<blockDim, nullptr, stream>>>(x, z, negativeSlope);
}
#endif
